﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.IO;

namespace GameBase
{
    public class MapInfo
    {
        private string _name;
        private Texture2D _background;
        private string _size;
        private List<Vector2> _positions;
        private List<int> _colorIndex;
        private List<bool> _orientations;

        public List<bool> Orientations
        {
            get { return _orientations; }
            set { _orientations = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public Texture2D Background
        {
            get { return _background; }
            set { _background = value; }
        }

        public string Size
        {
            get { return _size; }
            set { _size = value; }
        }

        public List<Vector2> Positions
        {
            get { return _positions; }
            set { _positions = value; }
        }

        public List<int> ColorIndex
        {
            get { return _colorIndex; }
            set { _colorIndex = value; }
        }

        public MapInfo(string strMap)
        {
            // <map>
            //  <name>Ground Yard</name>
            //  <background>backgound6</background>
            //  <size>Small</size>
            //  <teams>
            //    <team>
            //      <position>0,height</position>
            //      <color>1</color>
            //    </team>    
            //    <team>
            //      <position>width,height</position>
            //      <color>2</color>
            //    </team>
            //  </teams>
            //</map>

            _positions = new List<Vector2>();
            _colorIndex = new List<int>();
            _orientations = new List<bool>();

            try
            {
                // init xml reader
                XmlDocument doc = new XmlDocument();
                doc.Load(strMap);
                XmlElement root = doc.DocumentElement;

                // load map name
                XmlElement eleName = (XmlElement)root.SelectSingleNode("//name");
                _name = eleName.InnerText;

                // load background
                XmlElement eleBackground = (XmlElement)root.SelectSingleNode("//background");
                try
                {
                    _background = GameBase.Constant.content.Load<Texture2D>(@"Resources/" + eleBackground.InnerText);
                    // if can't load from Content Resources then load in Maps folder.
                }
                catch
                {
                    string strPathBackground = strMap.Substring(0, strMap.LastIndexOf('\\')) + "\\" + eleBackground.InnerText + ".jpg";
                    // Presumably in Update or LoadContent:
                    using (FileStream stream = File.OpenRead(strPathBackground))
                    {
                        _background = Texture2D.FromStream(Constant.graphics.GraphicsDevice, stream);
                    }
                }

                // load map size
                XmlElement eleSize = (XmlElement)root.SelectSingleNode("//size");
                _size = eleSize.InnerText;

                // load teams
                XmlElement eleTeams = (XmlElement)root.SelectSingleNode("//teams");
                XmlNodeList listNodeTeams = eleTeams.SelectNodes("//team");
                foreach (XmlNode node in listNodeTeams)
                {
                    XmlNodeList listNodeInTeam = node.ChildNodes;
                    foreach (XmlNode nodeChild in listNodeInTeam)
                    {
                        // load position
                        if (nodeChild.Name == "position")
                        {
                            string[] strPosition = nodeChild.InnerText.Split(',');
                            float X, Y;
                            if (strPosition[0] == "width")
                                X = GameBase.Constant.screenWidth;
                            else
                                X = float.Parse(strPosition[0]);
                            if (strPosition[1] == "height")
                                Y = GameBase.Constant.screenHeight;
                            else
                                Y = float.Parse(strPosition[1]);
                            _positions.Add(new Vector2(X, Y));
                        }
                        else

                            // load color
                            if (nodeChild.Name == "color")
                            {
                                _colorIndex.Add(Int32.Parse(nodeChild.InnerText));
                            }
                            else

                                // load orientation
                                if (nodeChild.Name == "orientation")
                                {
                                    _orientations.Add(nodeChild.InnerText == "right");
                                }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: Can't load map file: " + e.Message);
                throw (e);
            }
        }
    }
}
